/**
 * 
 */
package com.alkandros.linesmash.adapter;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alkandros.linesmash.R;
import com.alkandros.linesmash.models.RestaurantMenuItem;

/**
 * @author Jiju Induchoodan
 *
 */
public class ExpandableFoodAdapter  extends BaseExpandableListAdapter {
 
	int[] radio = new int[10];
	int[] check = new int[10];
	int selectedIndex = -1;
	private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<RestaurantMenuItem>> _listDataChild;
 
    public ExpandableFoodAdapter(Context context, List<String> listDataHeader,
            HashMap<String, List<RestaurantMenuItem>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }
 
    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }
 
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
 
    @Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        final RestaurantMenuItem model = (RestaurantMenuItem) getChild(groupPosition, childPosition);
        int _intPosition = childPosition;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item_child, null);
            final ChildViewHolder viewHolder = new ChildViewHolder();
            
            viewHolder.relativeItem 				= (RelativeLayout) convertView.findViewById(R.id.relative_item);
            viewHolder.txtListChild 				= (TextView) convertView.findViewById(R.id.lblListItem);
            viewHolder.txtPrice 					= (TextView) convertView.findViewById(R.id.lbl_price);
            viewHolder.imgArrow 					= (ImageView) convertView.findViewById(R.id.img_arrow);
            viewHolder.radioButton 					= (RadioButton) convertView.findViewById(R.id.radio_selection);
            viewHolder.checkBox	 					= (CheckBox) convertView.findViewById(R.id.check_box);
            convertView.setTag(viewHolder);
            viewHolder.radioButton.setTag(model);
      //      viewHolder.relativeItem.setTag(model);
        }
        else
        {
        	((ChildViewHolder) convertView.getTag()).radioButton.setTag(model);
        	//((ChildViewHolder) convertView.getTag()).relativeItem.setTag(model);
        }
        final ChildViewHolder holder = (ChildViewHolder) convertView.getTag();
        holder.txtListChild.setText(model.getName());
        holder.txtPrice.setText(model.getPrice());
        holder.imgArrow.setVisibility(View.INVISIBLE);
       
        holder.radioButton.setId(_intPosition);
        holder.radioButton.setTag(_intPosition);
        
          holder.relativeItem.setId(_intPosition);
     //   holder.relativeItem.setTag(_intPosition);
        holder.relativeItem.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) 
        	{
        		if(model.getType().equals("size"))
        		{
        			for (int i = 0; i < radio.length; i++) {
                        if (i == v.getId()) {
                        	radio[i] = 1;
                        } else {
                        	radio[i] = 0;
                        }
                    }
        		}
        		else
        		{
        			for (int i = 0; i < check.length; i++) {
        				if (i == v.getId()) 
        				{
        					 if (check[i] == 1)
                             {
                             	check[i] = 0;
                             } else {
                             	check[i] = 1;
                             }
        				}
                       
                    }
        		}
        		
                notifyDataSetChanged();
        	}
        });
        
        
        if(model.getType().equals("size"))
        {
        	holder.radioButton.setVisibility(View.VISIBLE);
        	holder.checkBox.setVisibility(View.INVISIBLE);
        	if (radio[_intPosition] == 1) {
                holder.radioButton.setChecked(true);
                holder.radioButton.setButtonDrawable(R.drawable.btn_radio_on_holo_light);
            } else {
                holder.radioButton.setChecked(false);
                holder.radioButton.setButtonDrawable(R.drawable.btn_radio_off_holo_light);
            }
        }
        else
        {
        	holder.checkBox.setVisibility(View.VISIBLE);
        	holder.radioButton.setVisibility(View.INVISIBLE);
        	
        	if (check[_intPosition] == 1) {
                holder.checkBox.setChecked(true);
                holder.checkBox.setButtonDrawable(R.drawable.apptheme_btn_check_on_holo_light);
            } else {
                holder.checkBox.setChecked(false);
                holder.checkBox.setButtonDrawable(R.drawable.apptheme_btn_check_off_holo_light);
            }
        }
        return convertView;
    }
  
    static class ChildViewHolder
    {
    	 RelativeLayout relativeItem;
    	 TextView txtListChild=null;
         TextView txtPrice=null;
         CheckBox checkBox =null;
         RadioButton radioButton=null;
         ImageView imgArrow;
    }
  
    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }
 
    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }
 
    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }
 
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
 
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }
        ImageView imgGroupIndicator = (ImageView) convertView.findViewById(R.id.group_indicator);
        TextView lblListHeader 		= (TextView) convertView.findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        if(isExpanded)
        	imgGroupIndicator.setBackgroundResource(R.drawable.option_arrow_down);
        else
        	imgGroupIndicator.setBackgroundResource(R.drawable.option_arrow);
 
        return convertView;
    }
 
    @Override
    public boolean hasStableIds() {
        return false;
    }
 
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
